Interfaces

Goal

Get sound to play with a button through a web server

Materials and software

  1. Xiao rp2040
  2. Esp32-c3
  3. Micropython
  4. Speaker
  5. Amplifier
  6. Jumper wires
  7. pcb board

Starting up

  1. First tried to send a serial signal from xiao rp2040 to my computer over the usb cable
  2. This proved extremely difficult because the documentation for micropython is not great and also could not get the right packages installed on the microcontroller
  3. Probably looked at everything related to xiaorp2040 and serial communication and could not get it to work with my mac.
  4. Not onyl did it not work but everytime I tried running code to test it blocked the port and i had to nuke flash the micro contorller manually
  5. Additionaly could not get tkinter or other packages to work to write a gui for my life, but finally got it to work with Qt5PYR something like that
  6. After nuke flashing manually the arduino like 10 times Anthony and I gave up and decided to try the esp32-c3
  7. I soldered and unsoldered my microcontorller so many times because I kept accdientally putting the wrong one it was embarassing
  8. I am extremely good at taking microcontorllers off a pcb board now
error
Processing error
gui
Tkinter Gui example
boards
What in the xiaorp2040

ESP32-C3 Communication

  1. What should be or is told to me is incredibly simple - sending commands via serial or wifi has been an incredible struggle in micropython
  2. I uploaded the code to esp32-c3 and building the web server was incredibly easy. Same process as the xiaorp2040
  3. Adapting the music code was also relatively easy as well - i just needed to change the pins and how I called them
  4. Now, just trying to communicate between the board and the web server is proving to be a huge challenge
  5. I cannot find any example so far that shows me how to do this and been struggling to understand how microcontrollers internally kinda work with communication
  6.     
            import network
    import socket
    import machine
    import threading
    from time import sleep
    
    ssid = 'EECS_Labs'
    password = ''
    #ssid = ''
    #password = ''
    
    
    # Define the pin connected to the LM4871 amplifier
    signal_pin = machine.Pin(10, machine.Pin.OUT)
    shutdown_pin = machine.Pin(9, machine.Pin.OUT)
    
    # Create a PWM object
    pwm = machine.PWM(signal_pin)
    
    # Set initial state of the PWM signal
    pwm.duty(0)  # Turn off the sound
    
    # Variable to track the state of the music
    music_playing = False
    
    def connect():
        # Connect to WLAN
        wlan = network.WLAN(network.STA_IF)
        wlan.active(True)
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            print('Waiting for connection...')
            sleep(1)
        ip = wlan.ifconfig()[0]
        print(f'Connected on {ip}')
        return ip
    
    def open_socket(ip):
        # Open a socket
        address = (ip, 80)
        connection = socket.socket()
        connection.bind(address)
        connection.listen(1)
        return connection
    
    def play_music():
        # Add your music playing logic here
        pwm.duty(300)  # Set duty cycle for sound
        pwm.freq(440)  # Set frequency (440 Hz is an example)
    
    def stop_music():
        # Turn off the sound
        pwm.duty(0)
    
    def play_music_thread():
        global music_playing
        while music_playing:
            play_music()
    
    def serve(connection):
        global music_playing
        # Start a web server
        while True:
            client = connection.accept()[0]
            request = client.recv(1024)
            request = str(request)
            try:
                request = request.split()[1]
            except IndexError:
                pass
            print('Received request:', request)
    
            if request == '/togglemusic':
                print('Toggling music...')
                if music_playing:
                    # Stop playing music
                    music_playing = False
                    stop_music()
                else:
                    # Start playing music in a separate thread
                    music_playing = True
                    threading.Thread(target=play_music_thread).start()
    
            client.send("HTTP/1.1 200 OK\nContent-Length: {}\n\n{}".format(len(html_css_code), html_css_code))
            client.close()
    
    try:
        # Set initial state of the shutdown pin
        shutdown_pin.off()  # Assuming high is off, adjust if necessary
    
        ip = connect()
        connection = open_socket(ip)
        pwm.duty(0)  # Turn off the sound
        serve(connection)
    
    except KeyboardInterrupt:
        # Set the shutdown pin to high before resetting
        shutdown_pin.on()  # Assuming high is off, adjust if necessary
        machine.reset()
    
    
        
    

    Final thoughts

    1. I eventually got the button to start taking in commands and sending them to the microcontroller
    2. There was a trailing ? when parsing web data that was getting in the way for a while
    3. I ran into more issues when trying to stop becuase the threading package was not wokring as expected
    4. Working with micropython and also trying to use chatgpt to debug is difficult due to the newness of micropython
    5. I eventually just got rid of the lines that were giving me trouble and it somehow still worked thankfully